// @ 516
// _(:з」∠)_

define([
    'table',
    'chart'
], function( Handsontable ) {
    'use strict';

    const Popup = {};

    // 提醒弹窗
    Popup["tipPopup"] = function( param ) {
        let body = document.body,
            popView = document.querySelector(".tipPopup");
        
        if ( !param.type ) {
            Popup.tipPopup({
                type: "toast",
                content: "参数不完全！",
                time: ""
            });
            return false;
        }

        let headView = ``,
            contView = ``;

        switch( param.type ) {
            case "toast":
                headView += `提醒`;
                contView += `${ param.content }`;
                break;
            case "alert":
                headView += `错误提醒`;
                contView += `${ param.content }`;
                break;
            case "confirm":
                headView += `请确认信息`;
                contView += `${ param.content }`;
                break;
            case "prompt":
                headView += `请输入信息`;
                contView += `${ param.content }`;
                break;
        }
        
        let frameView = `<div class="tipPopup ${ param.type }">
                                    <div class="popup_head">
                                        ${ headView }
                                        <span class="popup_close"></span>
                                    </div>
                                    <div class="popup_cont">${ contView }</div>
                                </div>`;

        if ( popView ) popView.remove();
        body.insertAdjacentHTML( "beforeEnd", frameView );

        // 关闭按钮操作
        popView = document.querySelector(".tipPopup");
        let closeBtn = popView.querySelector(".popup_close");
        closeBtn.addEventListener( 'click', function(e) {
            popView.remove();
        }, false);

        if ( param.type == "toast" ) {
            let timex = 2100;
            if ( param.time
                && param.time != ""
                && Number( param.time ) > 1000
            ) timex = Number( param.time );

            setTimeout(function() {
                popView = document.querySelector(".tipPopup");
                if ( popView ) popView.remove();
            }, timex);
        }
    }

    // 内容弹窗
    Popup["moreInfoPanel"] = function( areaCode, param, start, end ) {
        page = 2;
        let moreItemView = ``,
            popView = document.querySelector(".moreInfoPanel");

        if ( popView ) popView.remove();
        
        for ( let x = 0; x < param.length; x++ ) {
            var url = "";
            if( param[x].disaImgs == null ){
                url = "../image/image/noagrimage.png";
            }else {
                var imgArray = param[x].disaImgs.split(","); 
                    url = "http://118.25.235.213:8020/ChinaAPP/"+ imgArray[0];
            }
            var type = "";
            if( param[x].leve1Name == null ){
            }else {
              type += param[x].leve1Name;
            }
            if( param[x].descriptTypeName == null ){
            }else {
              type += ","+param[x].descriptTypeName;
            }
            moreItemView += `<li>
                                <div class="img">
                                    <img src="${ url }" onclick="showimgdiv('${ url }')"/>
                                </div>
                                <div class="info">
                                   <h title="${ param[x].userShowName }">用户名: ${ param[x].userShowName }</h>
                                   <div class="typediv">`
                                   for(let v=0;v<type.split(",").length;v++){
                                      if(v==0){
                                         moreItemView += `<div class="borderdiv">${ type.split(",")[v] }</div>`;
                                      }else{
                                         moreItemView += `<div class="borderdiv" style="margin-left: 5px;">${ type.split(",")[1] }</div>`;
                                      }
                                    }                                                     
                  moreItemView += `</div>
                                   <div class="disUploadInfo_des" title="${ param[x].descript }">${ param[x].descript }</div>
                                   <span style="width: 100%;">发布时间: ${ param[x].createtime.substring(0, 10) }</span>
                                   </div>
                            </li>`;
                            // <span style="width: 100%;">发布时间: ${ param[x].createtime.substring(0, 10) }</span>                
        }

        let moreInfoView = `<div class="moreInfoPanel">
                                <div class="moreInfo_head">
                                    <h>更多信息</h>
                                    <span class="moreInfo_close"></span>
                                </div>
                                <div class="moreInfo_cont">
                                    <ul class="moreInfo_list"> ${ moreItemView } </ul>
                                </div>
                            </div>`;

        document.querySelector(".main").insertAdjacentHTML("beforeEnd", moreInfoView);

        popView = document.querySelector(".moreInfoPanel");
        let closeBtn = popView.querySelector(".moreInfo_close"),
            moreInfo_scroll= popView.querySelector(".moreInfo_cont");

        closeBtn.addEventListener( 'click', function(e) {
            popView.remove();
        }, false);
        moreInfo_scroll.addEventListener( 'scroll', function(e) {
            var maxheight = $(".moreInfo_cont")[0].scrollHeight, //获取滚动条高度
                nowheight = $(".moreInfo_cont")[0].scrollTop,
                heights = $(".moreInfo_cont")[0].offsetHeight;
                if(nowheight+heights >= maxheight){
                    console.log("已经到底部了");
                    loadMore(areaCode, start, end);
                }
        }, false);
    }

    window.Popup = Popup;

    return Popup;
});

//滚轮到底加载更多
var page = 2;
function loadMore(areaCode, start, end ){    
    Loading.basic(); //加载图
    $.ajax({ 
        url: "http://118.25.235.213:8020/ChinaAPP/agrcollect/getPageAgrListByUserId?pageSize="+ page +"&pageNum=12&type=1&areaCode=" + areaCode+ "&beginTime=" + start + "&endTime=" + end,
        type: "GET",
        dataType: "json", 
        success: function(data) {
            Loading.remove(); 
            console.log(page);
            var param = data.data;
            if(param.length > 0){
                var contentHtml = "";
                for(x=0;x<param.length;x++){
                    var url = "";
                    if( param[x].disaImgs == null ){
                        url = "../image/image/noagrimage.png";
                    }else {
                        var imgArray = param[x].disaImgs.split(","); 
                            url = "http://118.25.235.213:8020/ChinaAPP/"+ imgArray[0];
                    }
            var type = "";
            if( param[x].leve1Name == null ){
            }else {
              type += param[x].leve1Name;
            }
            if( param[x].descriptTypeName == null ){
            }else {
              type += ","+param[x].descriptTypeName;
            }
            contentHtml += `<li>
                                <div class="img">
                                    <img src="${ url }" onclick="showimgdiv('${ url }')"/>
                                </div>
                                <div class="info">
                                   <h title="${ param[x].userShowName }">用户名: ${ param[x].userShowName }</h>
                                   <div class="typediv">`
                                   for(let v=0;v<type.split(",").length;v++){
                                      if(v==0){
                                        contentHtml += `<div class="borderdiv">${ type.split(",")[v] }</div>`;
                                      }else{
                                        contentHtml += `<div class="borderdiv" style="margin-left: 5px;">${ type.split(",")[1] }</div>`;
                                      }
                                    }                                                     
                  contentHtml += `</div>
                                   <div class="disUploadInfo_des" title="${ param[x].descript }">${ param[x].descript }</div>
                                   <span style="width: 100%;">发布时间: ${ param[x].createtime.substring(0, 10) }</span>
                                   </div>
                            </li>`;
                            // <span style="width: 100%;">发布时间: ${ param[x].createtime.substring(0, 10) }</span>                                                                           
                }  
                $(".moreInfo_list").append(contentHtml);  
                page++;           
            }else{
                Popup.tipPopup({
                    type: "toast",
                    content: "已经到底了"
                });
            }                                 
        },
        error: function(){

        }
    });
}

//展示大图
function showimgdiv(url){
    if( url.indexOf("http") >= 0){
            var imgView = `<div title="点击关闭图片" onclick="hideimgdiv()" class="disaimgdiv">
                             <img src="${url}">
                           </div>`;
        document.querySelector(".main").insertAdjacentHTML("beforeEnd", imgView);
    }
}

//隐藏
function hideimgdiv(){
    $(".disaimgdiv").remove();
}